package com.newer.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newer.domain.Supplier;
import com.newer.dto.LoginDto;
import com.newer.dto.SupplierDto;
import com.newer.dto.UserssDto;
import com.newer.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liruping
 * @version 1.0
 * Create by 2022/6/20 16:03
 */
@RestController
@RequestMapping("supplier")
public class supplierContriller {

    @Autowired
    SupplierService supplierService;


    //分页
    @RequestMapping("findSupplier")
    public Page<Supplier> SelectPages(SupplierDto supplierDto) {
        QueryWrapper<Supplier> queryWrapper =new QueryWrapper<>();
        //模糊查询
        if(supplierDto.getUname()!=null) {
            queryWrapper.like("suppliername",supplierDto.getUname());
        }

        queryWrapper.orderByDesc("supplierid");

        Page<Supplier> page =new Page<>(supplierDto.getPage(),supplierDto.getPageSize());
        this.supplierService.page(page,queryWrapper);

        System.out.println("当前页"+page.getCurrent());
        System.out.println("每页显示的行数"+page.getSize());
        System.out.println("总记录行数"+page.getTotal());
        System.out.println("总页数"+page.getPages());
        System.out.println("------------------------------");

        return  page;


    }

    //动态添加
    @RequestMapping("InsertSuppliter")
    public UserssDto InsertSuppliter(@RequestBody Supplier supplier) {

        this.supplierService.save(supplier);
        System.out.println(supplier);
        return new UserssDto(true,supplier);

    }

    //动态修改
    @RequestMapping("UpdateSuppliter")
    public UserssDto UpdateSuppliter(@RequestBody Supplier supplier) {

        this.supplierService.updateById(supplier);
        System.out.println(supplier);
        return new UserssDto(true,supplier);

    }

    //动态删除
    @RequestMapping("DeleteSuppliter")
    public UserssDto DeleteSuppliter(@RequestParam Integer supplierid) {

        this.supplierService.removeById(supplierid);
        System.out.println(supplierid);
        return new UserssDto(true,null);

    }



}
